package com.wzy.completable;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: wzy
 * @Date: 2021/11/27/11:54
 * @Description: 异步调用
 */
public class CompletableTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 异步调用无返回值
      CompletableFuture<Void> completableFuture1 = CompletableFuture.runAsync(() -> {
          System.out.println(Thread.currentThread().getName()+" --completableFuture1");
      });
        completableFuture1.get();
//        异步调用 有返回值
        CompletableFuture<Integer> completableFuture2 = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName()+"completableFuture2");
            return  1+1;
        });
        completableFuture2.whenComplete((x,y) ->{
            System.out.println(x);
            System.out.println(y);
        });
        System.out.println(completableFuture2.get());
    }
}

